package net.funtip.jdbc.driver;

import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
import java.sql.NClob;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.Ref;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.RowId;
import java.sql.SQLException;
import java.sql.SQLXML;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;

import net.funtip.jdbc.JDBCLogger;
import net.funtip.jdbc.entity.ParameterSet;
import net.funtip.jdbc.entity.ParameterSetImpl;

public class FTPreparedStatement extends FTStatement implements PreparedStatement {
	
	PreparedStatement realPreparedStatement;
	ParameterSet currentParams;

	public FTPreparedStatement(PreparedStatement statement, FTConnection parent, String sql) {
		super(statement, parent);
		realPreparedStatement = statement;
		this.lastSql = sql;
		this.currentParams = new ParameterSetImpl();
	}

	protected JDBCLogger getJdbcLogger() throws SQLException {
		return JDBCLogger.getInstance();
	}
	
	public void addBatch() throws SQLException {
//		getJdbcLogger().logParameter(lastSql,currentParams);
		realPreparedStatement.addBatch();
	}

	public void clearParameters() throws SQLException {
		currentParams.clear();

		realPreparedStatement.clearParameters();
	}

	public boolean execute() throws SQLException {
		boolean b;
		try {
			getJdbcLogger().startLogSqlQuery(lastSql);
			b = realPreparedStatement.execute();
			return b;
		} finally {
			getJdbcLogger().endLogSqlQuery(lastSql);
		}
	}

	public ResultSet executeQuery() throws SQLException {
		try {
			getJdbcLogger().startLogSqlQuery(lastSql);
			ResultSet r = realPreparedStatement.executeQuery();
			getJdbcLogger().logParameter(lastSql,currentParams);
			return new FTResultSet(r, this, lastSql);
		} finally {
			getJdbcLogger().endLogSqlQuery(lastSql);
		}
	}

	public int executeUpdate() throws SQLException {
		try {
			getJdbcLogger().startLogSqlQuery(lastSql);
			int i = realPreparedStatement.executeUpdate();
			return i;
		} finally {
			getJdbcLogger().endLogSqlQuery(lastSql);
		}
	}

	public ResultSetMetaData getMetaData() throws SQLException {
		return realPreparedStatement.getMetaData();
	}

	public void setArray(int i, Array x) throws SQLException {
		if (x instanceof FTSQLArray)
			realPreparedStatement.setArray(i, ((FTSQLArray) x).realArray);
		else
			realPreparedStatement.setArray(i, x);
	}

	public void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException {
		realPreparedStatement.setAsciiStream(parameterIndex, x, length);
	}

	public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException {
		realPreparedStatement.setBigDecimal(parameterIndex, x);
	}

	public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException {
		realPreparedStatement.setBinaryStream(parameterIndex, x, length);
	}

	public void setBlob(int i, Blob x) throws SQLException {
		realPreparedStatement.setBlob(i, x);
	}

	public void setBoolean(int parameterIndex, boolean x) throws SQLException {
		realPreparedStatement.setBoolean(parameterIndex, x);
	}

	public void setByte(int parameterIndex, byte x) throws SQLException {
		realPreparedStatement.setByte(parameterIndex, x);
	}

	public void setBytes(int parameterIndex, byte[] x) throws SQLException {
		realPreparedStatement.setBytes(parameterIndex, x);
	}

	public void setCharacterStream(int parameterIndex, Reader reader, int length) throws SQLException {
		realPreparedStatement.setCharacterStream(parameterIndex, reader, length);
	}

	public void setClob(int i, Clob x) throws SQLException {
		realPreparedStatement.setClob(i, x);
	}

	public void setDate(int parameterIndex, Date x) throws SQLException {
		currentParams.add(x);
		realPreparedStatement.setDate(parameterIndex, x);
	}

	public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException {
		currentParams.add(cal); 
		realPreparedStatement.setDate(parameterIndex, x, cal);
	}

	public void setDouble(int parameterIndex, double x) throws SQLException {
		realPreparedStatement.setDouble(parameterIndex, x);
	}

	public void setFloat(int parameterIndex, float x) throws SQLException {
		realPreparedStatement.setFloat(parameterIndex, x);
	}

	public void setInt(int parameterIndex, int x) throws SQLException {
		currentParams.add(x);
		realPreparedStatement.setInt(parameterIndex, x);
	}

	public void setLong(int parameterIndex, long x) throws SQLException {
		currentParams.add(x);
		realPreparedStatement.setLong(parameterIndex, x);
	}

	public void setNull(int parameterIndex, int sqlType) throws SQLException {
		realPreparedStatement.setNull(parameterIndex, sqlType);
	}

	public void setNull(int paramIndex, int sqlType, String typeName) throws SQLException {
		realPreparedStatement.setNull(paramIndex, sqlType, typeName);
	}

	public void setObject(int parameterIndex, Object x) throws SQLException {
		realPreparedStatement.setObject(parameterIndex, x);
	}

	public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException {
		realPreparedStatement.setObject(parameterIndex, x, targetSqlType);
	}

	public void setObject(int parameterIndex, Object x, int targetSqlType, int scale) throws SQLException {
		realPreparedStatement.setObject(parameterIndex, x, targetSqlType, scale);
	}

	public void setRef(int i, Ref x) throws SQLException {
		realPreparedStatement.setRef(i, x);
	}

	public void setShort(int parameterIndex, short x) throws SQLException {
		currentParams.add(x);
		realPreparedStatement.setShort(parameterIndex, x);
	}

	public void setString(int parameterIndex, String x) throws SQLException {
		realPreparedStatement.setString(parameterIndex, x);
	}

	public void setTime(int parameterIndex, Time x) throws SQLException {
		currentParams.add(x);
		realPreparedStatement.setTime(parameterIndex, x);
	}

	public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException {
		currentParams.add(x);
		realPreparedStatement.setTime(parameterIndex, x, cal);
	}

	public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException {
		currentParams.add(x);
		realPreparedStatement.setTimestamp(parameterIndex, x);
	}

	public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException {
		currentParams.add(x);
		realPreparedStatement.setTimestamp(parameterIndex, x, cal);
	}

	/**
	 * @deprecated
	 */
	public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException {
		realPreparedStatement.setUnicodeStream(parameterIndex, x, length);
	}

	/**
	 * @see java.sql.PreparedStatement#getParameterMetaData()
	 */
	public ParameterMetaData getParameterMetaData() throws SQLException {
		return realPreparedStatement.getParameterMetaData();
	}

	/**
	 * @see java.sql.PreparedStatement#setURL(int, URL)
	 */
	public void setURL(int arg0, URL arg1) throws SQLException {
		realPreparedStatement.setURL(arg0, arg1);
	}

	/**
	 * @see java.sql.Statement#execute(String, int)
	 */
	public boolean execute(String sql, int i) throws SQLException {
		try {
			getJdbcLogger().startLogSqlQuery(sql);
			return realPreparedStatement.execute(sql, i);
		} finally {
			getJdbcLogger().endLogSqlQuery(sql);
		}
	}

	/**
	 * @see java.sql.Statement#execute(String, int[])
	 */
	public boolean execute(String sql, int[] i) throws SQLException {
		try {
			getJdbcLogger().startLogSqlQuery(sql);
			return realPreparedStatement.execute(sql, i);
		} finally {
			getJdbcLogger().endLogSqlQuery(sql);
		}
	}

	/**
	 * @see java.sql.Statement#execute(String, String[])
	 */
	public boolean execute(String sql, String[] s) throws SQLException {
		try {
			getJdbcLogger().startLogSqlQuery(sql);
			return realPreparedStatement.execute(sql, s);
		} finally {
			getJdbcLogger().endLogSqlQuery(sql);
		}
	}

	/**
	 * @see java.sql.Statement#executeUpdate(String, int)
	 */
	public int executeUpdate(String sql, int i) throws SQLException {
		try {
			getJdbcLogger().startLogSqlQuery(sql);
			return realPreparedStatement.executeUpdate(sql, i);
		} finally {
			getJdbcLogger().endLogSqlQuery(sql);
		}
	}

	/**
	 * @see java.sql.Statement#executeUpdate(String, int[])
	 */
	public int executeUpdate(String sql, int[] i) throws SQLException {
		try {
			getJdbcLogger().startLogSqlQuery(sql);
			return realPreparedStatement.executeUpdate(sql, i);
		} finally {
			getJdbcLogger().endLogSqlQuery(sql);
		}
	}

	/**
	 * @see java.sql.Statement#executeUpdate(String, String[])
	 */
	public int executeUpdate(String sql, String[] s) throws SQLException {
		try {
			getJdbcLogger().startLogSqlQuery(sql);
			return realPreparedStatement.executeUpdate(sql, s);
		} finally {
			getJdbcLogger().endLogSqlQuery(sql);
		}
	}

	/**
	 * @see java.sql.Statement#getGeneratedKeys()
	 */
	public ResultSet getGeneratedKeys() throws SQLException {
		return realPreparedStatement.getGeneratedKeys();
	}

	/**
	 * @see java.sql.Statement#getMoreResults(int)
	 */
	public boolean getMoreResults(int arg0) throws SQLException {
		return realPreparedStatement.getMoreResults(arg0);
	}

	/**
	 * @see java.sql.Statement#getResultSetHoldability()
	 */
	public int getResultSetHoldability() throws SQLException {
		return realPreparedStatement.getResultSetHoldability();
	}

	public void setAsciiStream(int parameterIndex, InputStream x)
			throws SQLException {
		realPreparedStatement.setAsciiStream(parameterIndex, x);
	}

	public void setAsciiStream(int parameterIndex, InputStream x, long length)
			throws SQLException {
		realPreparedStatement.setAsciiStream(parameterIndex, x, length);
	}

	public void setBinaryStream(int parameterIndex, InputStream x)
			throws SQLException {
		realPreparedStatement.setBinaryStream(parameterIndex, x);
	}

	public void setBinaryStream(int parameterIndex, InputStream x, long length)
			throws SQLException {
		realPreparedStatement.setBinaryStream(parameterIndex, x, length);
	}

	public void setBlob(int parameterIndex, InputStream inputStream)
			throws SQLException {
		realPreparedStatement.setBlob(parameterIndex, inputStream);
	}

	public void setBlob(int parameterIndex, InputStream inputStream, long length)
			throws SQLException {
		realPreparedStatement.setBlob(parameterIndex, inputStream, length);		
	}

	public void setCharacterStream(int parameterIndex, Reader reader)
			throws SQLException {
		realPreparedStatement.setCharacterStream(parameterIndex, reader);
	}

	public void setCharacterStream(int parameterIndex, Reader reader,
			long length) throws SQLException {
		realPreparedStatement.setCharacterStream(parameterIndex, reader, length);
	}

	public void setClob(int parameterIndex, Reader reader) throws SQLException {
		realPreparedStatement.setClob(parameterIndex, reader);
	}

	public void setClob(int parameterIndex, Reader reader, long length)
			throws SQLException {
		realPreparedStatement.setClob(parameterIndex, reader, length);
	}

	public void setNCharacterStream(int parameterIndex, Reader value)
			throws SQLException {
		realPreparedStatement.setNCharacterStream(parameterIndex, value);
	}

	public void setNCharacterStream(int parameterIndex, Reader value,
			long length) throws SQLException {
		realPreparedStatement.setNCharacterStream(parameterIndex, value, length);
	}

	public void setNClob(int parameterIndex, NClob value) throws SQLException {
		realPreparedStatement.setNClob(parameterIndex, value);
	}

	public void setNClob(int parameterIndex, Reader reader) throws SQLException {
		realPreparedStatement.setNClob(parameterIndex, reader);
	}

	public void setNClob(int parameterIndex, Reader reader, long length)
			throws SQLException {
		realPreparedStatement.setNClob(parameterIndex, reader, length);
	}

	public void setNString(int parameterIndex, String value)
			throws SQLException {
		realPreparedStatement.setNString(parameterIndex, value);
	}

	public void setRowId(int parameterIndex, RowId x) throws SQLException {
		realPreparedStatement.setRowId(parameterIndex, x);
	}

	public void setSQLXML(int parameterIndex, SQLXML xmlObject)
			throws SQLException {
		realPreparedStatement.setSQLXML(parameterIndex, xmlObject);
	}

	public boolean isClosed() throws SQLException {
		return realPreparedStatement.isClosed();
	}

	public boolean isPoolable() throws SQLException {
		return realPreparedStatement.isPoolable();
	}

	public void setPoolable(boolean poolable) throws SQLException {
		realPreparedStatement.setPoolable(poolable);
	}

	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		return realPreparedStatement.isWrapperFor(iface);
	}

	public <T> T unwrap(Class<T> iface) throws SQLException {
		return realPreparedStatement.unwrap(iface);
	}

	@Override
	public void close() throws SQLException {
		super.close();
		connectionParent.onClosePreparedStatement(lastSql);
	}
}
